package crypter

import "bytes"

// PKCS7Padding 填充补全码
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize
	padText := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(ciphertext, padText...)
}

// PKCS7UnPadding 移除补全码
func PKCS7UnPadding(origData []byte, blockSize int) []byte {
	length := len(origData)
	if length == 0 {
		return origData
	}
	unPadding := int(origData[length-1])
	if length < unPadding || unPadding > blockSize {
		return origData
	}
	return origData[:(length - unPadding)]
}
